﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using papercut2.Data.Global;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
using System.IO;
using System.Windows.Forms;
using papercut2.Data.FileOperation;
using papercut2.Data.ImageProcess;

namespace papercut2.Data.FileOperation
{
    class FileControl
    {
        public  static Group OpenFile()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";
            openFile.FilterIndex = 2;
            openFile.ReadOnlyChecked = true;
            Group g = null;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                System.IO.FileStream fs = (System.IO.FileStream)openFile.OpenFile();
                IFormatter format = new BinaryFormatter();
                g = (Group)format.Deserialize(fs);
             //   MessageBox.Show(c.name);
            }
            return g;
        }
        public static void SaveFile(Group g)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";
            saveFile.FilterIndex = 2;
            saveFile.RestoreDirectory = true;

            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                //获得文件路径   
                string  localFilePath = saveFile.FileName.ToString();   

                //获取文件名，不带路径   
                string fileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);   

                //获取文件路径，不带文件名   
                string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));

                System.IO.FileStream fs = (System.IO.FileStream)saveFile.OpenFile();//输出文件   
                IFormatter format = new BinaryFormatter();
                format.Serialize(fs, g);
                fs.Close();
            }
       

        }




       
    }
}
